<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="同行名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入同行名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn" size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['seconded:job:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            class="sec-plain-btn" size="medium"
            icon="el-icon-download"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['seconded:job:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="jobList" border>
        <el-table-column
          label="同行名称"
          align="center"
          prop="name"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="联系人"
          align="center"
          prop="contact"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="联系电话"
          align="center"
          prop="phone"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="公司地址"
          align="center"
          prop="companyAddress"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="公司电话"
          align="center"
          prop="companyPhone"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="所属租户"
          align="center"
          prop="tenantName"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          fixed="left"
          align="center"
          class-name="small-padding fixed-width"
          width="140"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['seconded:job:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['seconded:job:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改借调同行对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      append-to-body
      width="550px"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="同行名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入同行名称"
                maxlength="20"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="联系人" prop="contact">
              <el-input
                v-model="form.contact"
                placeholder="请输入联系人"
                maxlength="10"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="联系电话" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入联系电话"
                maxlength="11"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="公司地址" prop="companyAddress">
              <el-input
                v-model="form.companyAddress"
                placeholder="请输入公司地址"
                maxlength="100"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="公司电话" prop="companyPhone">
              <el-input
                v-model="form.companyPhone"
                placeholder="请输入公司电话"
                maxlength="11"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                maxlength="20"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJob,
  getJob,
  delJob,
  addJob,
  updateJob,
  exportJob,
} from '@/api/seconded/job'

export default {
  name: 'Job',
  dicts: ['settlement_way'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 借调同行表格数据
      jobList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        contact: null,
        phone: null,
        companyAddress: null,
        companyPhone: null,
        settlementWay: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        settlementWay: [
          {
            required: true,
            message: '请选择结算方式',
            trigger: 'change',
          },
        ],
        name: [
          {
            required: true,
            message: '请输入同行名称',
            trigger: 'blur',
          },
        ],
        phone: [
          {
            pattern: /^(((\d{3,4}-)?[0-9]{7,8})|(1[3|4|5|6|7|8|9][0-9]\d{8}))$/,
            message: '请输入正确的联系电话',
            trigger: 'blur',
          },
        ],
        companyPhone: [
          {
            pattern: /^(((\d{3,4}-)?[0-9]{7,8})|(1[3|4|5|6|7|8|9][0-9]\d{8}))$/,
            message: '请输入正确的联系电话',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询借调同行列表 */
    getList() {
      this.loading = true
      listJob(this.queryParams).then((response) => {
        this.jobList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        contact: null,
        phone: null,
        companyAddress: null,
        companyPhone: null,
        settlementWay: null,
        tenantId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加借调同行'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getJob(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改借调同行'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateJob(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addJob(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除借调同行为"' + row.name + '"的数据项？')
        .then(function () {
          return delJob(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有借调同行数据项？')
        .then(() => {
          this.exportLoading = true
          return exportJob(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
